﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using VIP.Data;
using VIP.Data.Entities;
using VIP.Domain;
using VIP.Infrastructure;
using VIP.Infrastructure.Caching;

namespace WebApplication.Controllers
{
    /// <summary>
    /// 用户管理中心
    /// </summary>
    [ApiController]
    [Route("BaseData/[controller]")]
    public class UserController : ControllerBase
    {
        private readonly ILogger<UserController> _logger;
        private readonly VIPDbContext sqlDbRWContext;
        private readonly VIPDbReadOnlyContext sqlReadContext;
        private readonly IOptions<DomainOptions> _options;
        private readonly ICachingProvider _cachingProvider;
        private readonly IVIPHttpContext _httpContext;

        public UserController(ILogger<UserController> logger, VIPDbContext context, VIPDbReadOnlyContext readContext, IOptions<DomainOptions> options, ICachingProvider cachingProvider, IVIPHttpContext httpContext)
        {
            _logger = logger;
            sqlDbRWContext = context;
            sqlReadContext = readContext;
            _options = options;
            _cachingProvider = cachingProvider;
            _httpContext = httpContext;
        }
        /// <summary>
        /// 获取用户id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Get")]
        [HttpGet]
        public IEnumerable<int> Get(int id)
        {
            return new int[] { id };
        }

        [HttpGet]
        [Route("Create")]
        public bool Create()
        {
            sqlDbRWContext.Add(new User()
            {
                Name = new Random(DateTime.Now.Millisecond).Next(1, 100).ToString()
            }); ;
            sqlDbRWContext.SaveChanges();
            return true;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Query")]
        public User Query(int id)
        {
            Thread.Sleep(2000);
            var userEntity = sqlReadContext.Set<User>().AsQueryable().Where(p => p.Eid == id).FirstOrDefault();
            return userEntity;
        }

        [HttpPost]
        [Route("DomainOptions")]
        public string DomainOptions()
        {
            return _options.Value.OrderCachePrefix;
        }
        [HttpPost]
        [Route("CacheSet")]
        public Task<bool> CacheSet()
        {
            return _cachingProvider.SetAsync("lvcc", new User()
            {
                Deleted = false,
                Eid = 1,
                Name = "lvcc"
            });
        }
        [HttpPost]
        [Route("CacheGet")]
        public Task<ApiResponse<User>> CacheGet()
        {
            var rel = _cachingProvider.Get<User>("lvcc");
            return ApiRes.OKAsync(rel);
        }
        [HttpPost]
        [Route("GetSession")]
        public ApiResponse<string> GetSession()
        {
            var sn = _httpContext.Session;
            return ApiRes.OK(_httpContext.Session);
        }
    }
}
